<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="econt_form">
  <div class="content">
    <table>
      <tr <?php if (!$cd) { ?> style="display: none;"<?php } ?> style="display:none;">
        <td width="120"><?php echo $entry_cd; ?></td>
        <td><input type="radio" id="cd_yes" name="cd_payment" value="1" <?php if ($cd_payment) { ?> checked="checked"<?php } ?> />
          <label for="cd_yes"><?php echo $text_yes; ?></label>
          <input type="radio" id="cd_no" name="cd_payment" value="0" <?php if (!$cd_payment) { ?> checked="checked"<?php } ?> />
          <label for="cd_no"><?php echo $text_no; ?></label></td>
      </tr>
      <tr <?php if (!$to_door || !$to_office) { ?> style="display: none;"<?php } ?>>
        <td><?php echo $entry_shipping_to; ?></td>
        <td><input type="radio" id="to_door" name="shipping_to" value="DOOR" <?php if ($shipping_to != 'OFFICE') { ?> checked="checked"<?php } ?> onclick="$('#econt_office_city_id,#econt_office_id,#econt_office_code,#econt_office_locator').hide(); $('#econt_post_code,#econt_city,#econt_quarter,#econt_street,#econt_street_num,#econt_other').show();" />
          <label for="to_door"><?php echo $text_to_door; ?></label>
          <input type="radio" id="to_office" name="shipping_to" value="OFFICE" <?php if ($shipping_to == 'OFFICE') { ?> checked="checked"<?php } ?> onclick="$('#econt_office_city_id,#econt_office_id,#econt_office_code,#econt_office_locator').show();$('#econt_post_code,#econt_city,#econt_quarter,#econt_street,#econt_street_num,#econt_other').hide();" />
          <label for="to_office"><?php echo $text_to_office; ?></label></td>
      </tr>
      <tr>
        <td><label for="company"><?php echo $entry_company; ?></label></td>
        <td><input type="text" id="company" name="company" value="<?php echo $company; ?>" /></td>
      </tr>
      <tr id="econt_post_code" <?php if ($shipping_to == 'OFFICE' || !$to_door) { ?> style="display: none;"<?php } ?>>
        <td><label for="post_code"><?php echo $entry_post_code; ?></label></td>
        <td><input type="text" id="post_code" name="postcode" value="<?php echo $postcode; ?>" size="3" disabled="disabled" /></td>
      </tr>
      <tr id="econt_city" <?php if ($shipping_to == 'OFFICE' || !$to_door) { ?> style="display: none;"<?php } ?>>
        <td><label for="city"><?php echo $entry_city; ?></label></td>
        <td><input type="text" id="city" name="city" value="<?php echo $city; ?>" />
          <input type="hidden" id="city_id" name="city_id" value="<?php echo $city_id; ?>" /></td>
      </tr>
      <tr id="econt_quarter" <?php if ($shipping_to == 'OFFICE' || !$to_door) { ?> style="display: none;"<?php } ?>>
        <td><label for="quarter"><?php echo $entry_quarter; ?></label></td>
        <td><input type="text" id="quarter" name="quarter" value="<?php echo $quarter; ?>" /></td>
      </tr>
      <tr id="econt_street" <?php if ($shipping_to == 'OFFICE' || !$to_door) { ?> style="display: none;"<?php } ?>>
        <td><label for="street"><?php echo $entry_street; ?></label></td>
        <td><input type="text" id="street" name="street" value="<?php echo $street; ?>" /></td>
      </tr>
      <tr id="econt_street_num" <?php if ($shipping_to == 'OFFICE' || !$to_door) { ?> style="display: none;"<?php } ?>>
        <td><label for="street_num"><?php echo $entry_street_num; ?></label></td>
        <td><input type="text" id="street_num" name="street_num" value="<?php echo $street_num; ?>" /></td>
      </tr>
      <tr id="econt_other" <?php if ($shipping_to == 'OFFICE' || !$to_door) { ?> style="display: none;"<?php } ?>>
        <td><label for="other"><?php echo $entry_other; ?></label></td>
        <td><input type="text" id="other" name="other" value="<?php echo $other; ?>" />
          <?php if ($error_address) { ?>
          <span class="error"><?php echo $error_address; ?></span>
          <?php } ?></td>
      </tr>
      <tr id="econt_office_city_id" <?php if ($shipping_to != 'OFFICE' || !$to_office) { ?> style="display: none;"<?php } ?>>
        <td><label for="office_city_id"><?php echo $entry_city; ?></label></td>
        <td><select id="office_city_id" name="office_city_id" onchange="getOfficesByCityId();">
            <option value="0"><?php echo $text_select; ?></option>
            <?php foreach ($cities as $city) { ?>
            <?php if ($city['city_id'] == $office_city_id) { ?>
            <option value="<?php echo $city['city_id']; ?>" selected="selected"><?php echo $city['name']; ?></option>
            <?php } else { ?>
            <option value="<?php echo $city['city_id']; ?>"><?php echo $city['name']; ?></option>
            <?php } ?>
            <?php } ?>
          </select></td>
      </tr>
      <tr id="econt_office_id" <?php if ($shipping_to != 'OFFICE' || !$to_office) { ?> style="display: none;"<?php } ?>>
        <td><label for="office_id"><?php echo $entry_office; ?></label></td>
        <td><select id="office_id" name="office_id" onchange="getOffice();" style="width: 400px;">
            <option value="0"><?php echo $text_select; ?></option>
            <?php foreach ($offices as $office) { ?>
            <?php if ($office['office_id'] == $office_id) { ?>
            <option value="<?php echo $office['office_id']; ?>" selected="selected"><?php echo $office['office_code'] . ', ' . $office['name'] . ', ' . $office['address']; ?></option>
            <?php } else { ?>
            <option value="<?php echo $office['office_id']; ?>"><?php echo $office['office_code'] . ', ' . $office['name'] . ', ' . $office['address']; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
          <?php if ($error_office) { ?>
          <span class="error"><?php echo $error_office; ?></span>
          <?php } ?></td>
      </tr>
      <tr id="econt_office_code" <?php if ($shipping_to != 'OFFICE' || !$to_office) { ?> style="display: none;"<?php } ?>>
        <td><label for="office_code"><?php echo $entry_office_code; ?></label></td>
        <td><input type="text" id="office_code" name="office_code" value="<?php echo $office_code; ?>" size="3" disabled="disabled" /></td>
      </tr>
      <tr id="econt_office_locator" <?php if ($shipping_to != 'OFFICE' || !$to_office) { ?> style="display: none;"<?php } ?>>
        <td>&nbsp;</td>
        <td><a href="javascript:void(0);" id="office_locator" class="button" title="<?php echo $button_office_locator; ?>"><?php echo $button_office_locator; ?></a></td>
      </tr>
      <tr id="priority_time" <?php if (!$priority_time) { ?> style="display: none;"<?php } ?>>
        <td colspan="2"><input type="checkbox" id="priority_time_cb" name="priority_time_cb" value="1" <?php if ($priority_time_cb) { ?> checked="checked"<?php } ?> onclick="checkPriorityTime();" />
          <label for="priority_time_cb"><?php echo $entry_priority_time; ?></label>
          <select id="priority_time_type_id" name="priority_time_type_id" <?php if (!$priority_time_cb) { ?> disabled="disabled"<?php } ?> onchange="setPriorityTime();">
            <?php foreach ($priority_time_types as $priority_time_type) { ?>
            <?php if ($priority_time_type['id'] == $priority_time_type_id) { ?>
            <?php $priority_time_hours = $priority_time_type['hours']; ?>
            <option value="<?php echo $priority_time_type['id']; ?>" selected="selected"><?php echo $priority_time_type['name']; ?></option>
            <?php } else { ?>
            <option value="<?php echo $priority_time_type['id']; ?>"><?php echo $priority_time_type['name']; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
          <select id="priority_time_hour_id" name="priority_time_hour_id" <?php if (!$priority_time_cb) { ?> disabled="disabled"<?php } ?>>
            <?php foreach ($priority_time_hours as $priority_time_hour) { ?>
            <?php if ($priority_time_hour == $priority_time_hour_id) { ?>
            <option value="<?php echo $priority_time_hour; ?>" selected="selected"><?php echo $priority_time_hour; ?></option>
            <?php } else { ?>
            <option value="<?php echo $priority_time_hour; ?>"><?php echo $priority_time_hour; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
          <label for="priority_time_hour_id"><?php echo $text_hour; ?></label>
          <?php if ($error_priority_time) { ?>
          <span class="error"><?php echo $error_priority_time; ?></span>
          <?php } ?></td>
      </tr>
      <tr id="express_city_courier" <?php if (!$express_city_courier) { ?> style="display: none;"<?php } ?> style="display:none">
        <td colspan="2"><input type="checkbox" id="express_city_courier_cb" name="express_city_courier_cb" value="1" <?php if ($express_city_courier_cb) { ?> checked="checked"<?php } ?> onclick="checkExpressCityCourier();" />
          <label for="express_city_courier_cb"><?php echo $entry_express_city_courier; ?></label>
          <input type="radio" id="express_city_courier_e1" name="express_city_courier_e" value="e1" <?php if ($express_city_courier_e == 'e1') { ?> checked="checked"<?php } ?> <?php if (!$express_city_courier_cb) { ?> disabled="disabled"<?php } ?> />
          <label for="express_city_courier_e1"><?php echo $text_e1; ?></label>
          <input type="radio" id="express_city_courier_e2" name="express_city_courier_e" value="e2" <?php if ($express_city_courier_e == 'e2') { ?> checked="checked"<?php } ?> <?php if (!$express_city_courier_cb) { ?> disabled="disabled"<?php } ?> />
          <label for="express_city_courier_e2"><?php echo $text_e2; ?></label>
          <input type="radio" id="express_city_courier_e3" name="express_city_courier_e" value="e3" <?php if ($express_city_courier_e == 'e3') { ?> checked="checked"<?php } ?> <?php if (!$express_city_courier_cb) { ?> disabled="disabled"<?php } ?> />
          <label for="express_city_courier_e3"><?php echo $text_e3; ?></label></td>
      </tr>
      <tr <?php if (!$delivery_day || !$delivery_days && !$error_delivery_day) { ?> style="display: none;"<?php } ?>>
        <td><label for="delivery_day_id"><?php echo $entry_delivery_day; ?></label></td>
        <td><select id="delivery_day_id" name="delivery_day_id" onchange="changeDeliveryDay();">
            <?php foreach ($delivery_days as $delivery_day) { ?>
            <?php if ($delivery_day['id'] == $delivery_day_id) { ?>
            <option value="<?php echo $delivery_day['id']; ?>" selected="selected"><?php echo $delivery_day['name']; ?></option>
            <?php } else { ?>
            <option value="<?php echo $delivery_day['id']; ?>"><?php echo $delivery_day['name']; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
          <?php if ($error_delivery_day) { ?>
          <span class="error"><?php echo $error_delivery_day; ?></span>
          <?php } ?></td>
      </tr>
    </table>
  </div>
  <div class="buttons">
    <div class="left"><b><?php echo $text_calculate; ?></b></div>
    <div class="right"><input type="button" value="<?php echo $button_calculate; ?>" id="button-econt-calculate" onclick="econtSubmit();" class="button" /></div>
  </div>
  <div class="content">
    <?php if ($dc) { ?>
    <p><i><?php echo $text_dc; ?></i></p>
    <?php } ?>
    <?php if ($dc_cp) { ?>
    <p><i><?php echo $text_dc_cp; ?></i></p>
    <?php } ?>
    <?php if ($invoice_before_cd) { ?>
    <p><i><?php echo $text_invoice_before_cd; ?></i></p>
    <?php } ?>
    <?php if ($pay_after_accept) { ?>
    <p><i><?php echo $text_pay_after_accept; ?></i></p>
    <?php } ?>
    <?php if ($pay_after_test) { ?>
    <p><i><?php echo $text_pay_after_test; ?></i></p>
    <?php } ?>
    <?php if ($instruction_shipping_returns) { ?>
    <p><i><?php echo $text_instruction_shipping_returns; ?></i></p>
    <?php } ?>
    <?php if ($instruction_returns) { ?>
    <p><i><?php echo $text_instruction_returns; ?></i></p>
    <?php } ?>
    <?php if ($partial_delivery) { ?>
    <p><b><?php echo $text_partial_delivery; ?></b></p>
    <?php } ?>
  </div>
</form>


<script type="text/javascript"><!--
	function receiveMessage(event) {
		if (event.origin !== '<?php echo $office_locator_domain; ?>')
			return;

		message_array = event.data.split('||');
		getOfficeByOfficeCode(message_array[0]);
		$.colorbox.close();
	}

	if (window.addEventListener) {
		window.addEventListener('message', receiveMessage, false);
	} else if (window.attachEvent) {
		window.attachEvent('onmessage', receiveMessage);
	}

	$(document).ready(function() {
		
		
		$('#content').on('click', '#office_locator',function() {
			
			if ($('#office_city_id').val()) {
				url = '<?php echo $office_locator; ?>&address=' + $('#office_city_id option:selected').text();
			} else {
				url = '<?php echo $office_locator; ?>';
			}
			
			$.colorbox({
				overlayClose: true,
				href : url,
				iframe : true,
				opacity: 0.5,
				width  : '1000',
				height : '700'
			});
		
		});
		
		$('#content').on('change', '#office_city_id',function() {
		
		//$('#office_city_id').change(function () {
			if ($('#office_city_id').val()) {
				url = '<?php echo $office_locator; ?>&address=' + $('#office_city_id option:selected').text();
			} else {
				url = '<?php echo $office_locator; ?>';
			}

			$.colorbox({
				overlayClose: true,
				href : url,
				iframe : true,
				opacity: 0.5,
				width  : '1000',
				height : '700'
			});
		});
	});

	function getOfficeByOfficeCode(office_code) {
		if (parseInt(office_code)) {
			$.ajax({
				url: 'index.php?route=shipping/econt/getOfficeByOfficeCode',
				type: 'POST',
				data: 'office_code=' + parseInt(office_code),
				dataType: 'json',
				success: function(data) {
					if (!data.error) {
						$('#office_city_id').val(data.city_id);
						html = '<option value="0"><?php echo $text_select; ?></option>';

						for (i = 0; i < data.offices.length; i++) {
							html += '<option ';
							if (data.offices[i]['office_id'] == data.office_id) {
								html += 'selected="selected"';
							}
							html += 'value="' + data.offices[i]['office_id'] + '">' + data.offices[i]['office_code'] + ', ' + data.offices[i]['name'] + ', ' + data.offices[i]['address'] +  '</option>';
						}

						$('#office_id').html(html);
						$('#office_code').val(office_code);
					}
				}
			});
		}
	}
//--></script>
<script type="text/javascript"><!--
var priority_time = '<?php echo $priority_time; ?>';

$(document).ready(function() {
	if ($('#delivery_day_id').is(':visible')) {
		changeDeliveryDay();
	}
});

function econtSubmit() {
	$('#loading').remove();
	$('#econt_form').prepend('<div id="loading" class="wait"><img src="catalog/view/theme/kipo/image/loader.gif" alt="" /></div>');

	$('#econt_form :input').removeAttr('disabled');

	$.ajax({
		url: 'index.php?route=shipping/econt',
		type: 'POST',
		data: $('#econt_form').serialize(),
		dataType: 'json',
		success: function(data) {
			if (data) {
				if (data.redirect) {
					location = data.redirect;
				} else if (data.submit) {
					$.ajax({
						url: 'index.php?route=checkout/shipping_method/validate',
						type: 'post',
						data: $('#shipping-method input[type=\'radio\']:checked, #shipping-method textarea'),
						dataType: 'json',
						beforeSend: function() {
							$('#button-econt-calculate').attr('disabled', true);
							$('#button-shipping-method').attr('disabled', true);
							$('#button-shipping-method').after('<span class="wait">&nbsp;<img src="catalog/view/theme/kipo/image/loader.gif" alt="" /></span>');
						},
						complete: function() {
							$('#button-econt-calculate').attr('disabled', false);
							$('#button-shipping-method').attr('disabled', false);
						},
						success: function(json) {
							$('.wait').remove();
							$('.warning').remove();

							if (json['redirect']) {
								location = json['redirect'];
							}

							if (json['error']) {
								if (json['error']['warning']) {
									$('#shipping-method .checkout-content').prepend('<div class="warning" style="display: none;">' + json['error']['warning'] + '</div>');

									$('.warning').fadeIn('slow');
								}
							} else {
								$('#econt_form').prepend('<div class="wait"><img src="catalog/view/theme/kipo/image/loader.gif" alt="" /></div>');

								$.ajax({
									url: 'index.php?route=checkout/shipping_method',
									dataType: 'html',
									success: function(html) {
										$('.wait').remove();
										$('#shipping-method .checkout-content').html(html);
									}
								});
							}
						}
					});
				} else {
					$('#econt').html(data.html);
				}
			}
		}
	});
}

function getOfficesByCityId() {
	$('#office_id').html('<option value="0"><?php echo $text_wait; ?></option>');
	$('#office_code').val('');

	$.ajax({
		url: 'index.php?route=shipping/econt/getOfficesByCityId',
		type: 'POST',
		data: 'city_id=' + encodeURIComponent($('#office_city_id').val()),
		dataType: 'json',
		success: function(data) {
			if (data) {
				html = '<option value="0"><?php echo $text_select; ?></option>';

				for (i = 0; i < data.length; i++) {
					html += '<option value="' + data[i]['office_id'] + '">' + data[i]['office_code'] + ', ' + data[i]['name'] + ', ' + data[i]['address'] +  '</option>';
				}

				$('#office_id').html(html);
			}
		}
	});
}

function getOffice() {
	$('#office_code').val('');

	$.ajax({
		url: 'index.php?route=shipping/econt/getOffice',
		type: 'POST',
		data: 'office_id=' + encodeURIComponent($('#office_id').val()),
		dataType: 'json',
		success: function(data) {
			if (data) {
				$('#office_code').val(data.office_code);
			}
		}
	});
}

function checkPriorityTime() {
	if ($('#priority_time_cb:checked').length) {
		$('#priority_time_type_id').removeAttr('disabled');
		$('#priority_time_hour_id').removeAttr('disabled');
	} else {
		$('#priority_time_type_id').attr('disabled', 'disabled');
		$('#priority_time_hour_id').attr('disabled', 'disabled');
	}
}

function setPriorityTime() {
	var type = $('#priority_time_type_id').val();
	var hour = $('#priority_time_hour_id').val();

	var html = '<option value="10">10</option>';
	html += '<option value="11">11</option>';
	html += '<option value="12">12</option>';
	html += '<option value="13">13</option>';
	html += '<option value="14">14</option>';
	html += '<option value="15">15</option>';
	html += '<option value="16">16</option>';
	html += '<option value="17">17</option>';

	if (type == 'BEFORE') {
		$('#priority_time_hour_id').html(html + '<option value="18">18</option>');
	} else if (type == 'IN') {
		$('#priority_time_hour_id').html('<option value="9">9</option>' + html + '<option value="18">18</option>');
	} else if (type == 'AFTER') {
		$('#priority_time_hour_id').html('<option value="9">9</option>' + html);
	}

	$('#priority_time_hour_id').val(hour).attr('selected', 'selected');
}

function checkExpressCityCourier() {
	if ($('#express_city_courier_cb:checked').length) {
		$('#express_city_courier_e1').removeAttr('disabled');
		$('#express_city_courier_e2').removeAttr('disabled');
		$('#express_city_courier_e3').removeAttr('disabled');
	} else {
		$('#express_city_courier_e1').attr('disabled', 'disabled');
		$('#express_city_courier_e2').attr('disabled', 'disabled');
		$('#express_city_courier_e3').attr('disabled', 'disabled');
	}
}

function changeDeliveryDay() {
	if ($('#delivery_day_id').val() == '<?php echo $priority_date; ?>') {
		$('#priority_time').show();

		if (!$('#priority_time_cb:checked').length) {
			$('#priority_time_cb').attr('checked', true);
			$('#priority_time_type_id').removeAttr('disabled');
			$('#priority_time_type_id').val('BEFORE').attr('selected', 'selected');
			$('#priority_time_hour_id').removeAttr('disabled');
			$('#priority_time_hour_id').val('13');
		}
	} else {
		if (!parseInt(priority_time)) {
			$('#priority_time').hide();
		}

		$('#priority_time_cb').attr('checked', false);
		$('#priority_time_type_id').attr('disabled', 'disabled');
		$('#priority_time_hour_id').attr('disabled', 'disabled');
	}
}
//--></script>

<link rel="stylesheet" type="text/css" href="catalog/view/javascript/jquery/ui/themes/ui-lightness/jquery.autocomplete.css" />
<script type="text/javascript" src="catalog/view/javascript/jquery/ui/jquery.autocomplete.js"></script>

<script type="text/javascript"><!--
$(document).ready(function() {
	var sender_post_code = '<?php echo $sender_post_code; ?>';
	
	
		
	$('#city').autocomplete('index.php?route=shipping/econt/getCitiesByName', {
		minChars: 0,
		mustMatch: true,
		width: 200,
		formatItem: function(row) {
			return row[1] + ' ' + row[0];
		},
		formatMatch: function(row) {
			return row[0];
		},
		formatResult: function(row) {
			return row[0];
		}
	}).result(function(event, row) {
		if (row) {
			$('#post_code').val(row[1]);
			$('#city_id').val(row[2]);

			if (row[1] == sender_post_code) {
				$('#express_city_courier').show();
			} else {
				$('#express_city_courier').hide();
			}
		} else {
			$('#post_code').val('');
			$('#city_id').val('');
			$('#express_city_courier').hide();
		}
	});
	

	$('#quarter').autocomplete('index.php?route=shipping/econt/getQuartersByName', {
		minChars: 0,
		mustMatch: true,
		width: 200,
		extraParams: {
			city_id: function() { return $('#city_id').val(); }
		}
	});

	$('#street').autocomplete('index.php?route=shipping/econt/getStreetsByName', {
		minChars: 0,
		mustMatch: true,
		width: 200,
		extraParams: {
			city_id: function() { return $('#city_id').val(); }
		}
	});

	
	$('#content').on('change', 'input[name=\'city\']',function() {
	//$('input[name=\'city\']').change(function() {
		$('input[name=\'quarter\'], input[name=\'street\'], input[name=\'street_num\'], input[name=\'other\']').val('');
	});
});
//--></script>